﻿using Microsoft.Extensions.DependencyInjection;
using Modules.BankSpecialAccount.Entitys;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Caching;
using PmSoft.DBContext;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.BankSpecialAccount.Repositories;

/// <summary>
/// 专户累计进账、线上和线下代发金额统计每月统计仓储
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class BankSpecialAccountStatisticMonthRepository : Repository<BankSpecialAccountStatisticMonth, DefaultDbContext>
{
    public BankSpecialAccountStatisticMonthRepository(DefaultDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
    {
    }

    /// <summary>
    /// 通过银行专户账号获取统计信息
    /// </summary>
    /// <param name="account"></param>
    /// <returns></returns>
    public BankSpecialAccountStatisticMonth? GetProjectSpecialAccountStatisticMonthByBankAccountMonth(string account, int month)
    {
        return GetProjectSpecialAccountStatisticMonthsByBankAccountMonth(account, month).FirstOrDefault();
        //Sql sql = Sql.Builder;
        //sql.Select("id").From("project_special_account_statistic").Where("bank_account=@0", account);
        //var id = DbContext.FirstOrDefault<int>(sql);
        //return Get(id);
    }

    public IEnumerable<BankSpecialAccountStatisticMonth> GetProjectSpecialAccountStatisticMonthsByBankAccountMonth(string account, int month)
    {
        return GetTopEntities(100, CachingExpirationType.UsualSingleObject, () =>
        {
            int areaVersion = EntityCacheService.GetAreaVersion("BankAccount", account);
            return $"ProjectSpecialAccountStatisticMonth:{account}:{areaVersion}";
        }, () =>
        {
            Sql sql = Sql.Builder;
            sql.Select("id").From("project_special_account_statistic_month").Where("bank_account=@0 and month=@1", account, month);
            return sql;
        });
        //Sql sql = Sql.Builder;
        //sql.Select("id").From("project_special_account_statistic").Where("bank_account=@0", account);
        //var id = DbContext.FirstOrDefault<int>(sql);
        //return Get(id);
    }
}